<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:member:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_member_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:member:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.keyword" :placeholder="i18n('common.please_input') + i18n('common.keyword')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.organization_id" :placeholder="i18n('common.please_select') + i18n('member.organization_info')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.chinese_title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.role_id" :placeholder="i18n('common.please_select') + i18n('member.role_title')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in roleList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.market_status" :placeholder="i18n('common.please_select') + i18n('member.market_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option :label="i18n('common.yes')" value="1"></el-option>
            <el-option :label="i18n('common.no')" value="2"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.verify_status" :placeholder="i18n('common.please_select') + i18n('member.verify_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in verifyStatusList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.last_login_time" type="daterange" :range-separator="i18n('common.to')" :start-placeholder="i18n('common.start_time')" :end-placeholder="i18n('common.end_time')" clearable></el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('member.organization_info')" min-width="200">
            <template v-slot="scope">
              <span v-if="scope.row.organization">{{ scope.row.organization.chinese_title }}</span>
              <span v-else>个人会员</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('member.role_title')" width="100">
            <template v-slot="scope">
              <span v-if="scope.row.role">{{ scope.row.role.title }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="username" :label="i18n('member.username')" width="120">
          </el-table-column>

          <el-table-column :label="i18n('member.info')" min-width="300">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.avatar" :lazy="true">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('member.chinese_nickname') }}： {{ scope.row.chinese_nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('member.create_time') }}： {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('member.valid_time')" width="200">
            <template v-slot="scope">
              <el-text v-if="scope.row.contract">
                {{ scope.row.contract.valid_time_text }}
              </el-text>
              <el-text v-else>
                暂无信息
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="last_login_time" :label="i18n('member.last_login_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('member.market_status')" width="80">
            <template v-slot="scope">
              <el-text>
                {{ scope.row.market_status.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('member.verify_status')" width="80">
            <template v-slot="scope">
              <el-text>
                {{ scope.row.verify_status.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('member.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:member:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_member_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:member:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="isAuth('module:member:contract:form')" @click="router.push({ name: 'module_member_contract_form', query: { member_id: scope.row.id } })">
                      {{ i18n('member.contract.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:member:refuel:form')" @click="router.push({ name: 'module_member_refuel_form', query: { member_id: scope.row.id } })">
                      {{ i18n('member.refuel.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:member:distribution:list') && scope.row.market_status == 1" @click="router.push({ name: 'module_member_distribution_list', query: { inviter_id: scope.row.id } })">
                      {{ i18n('member.distribution.info') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:member:qrcode')" @click="dialogVisible1 = true;qrcode_url = '';loadQrcodeUrl(scope.row.id)">
                      {{ i18n('member.mini_code') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:member:qrcode')" @click="dialogVisible2 = true;placard_url = '';loadQrcodeUrl(scope.row.id)">
                      {{ i18n('member.mini_placard') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:member:qrcode')" @click="dialogVisible3 = true;dm_url = '';loadQrcodeUrl(scope.row.id)">
                      {{ i18n('member.placard') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible1" title="邀请小程序码" width="400" center>
      <div class="text-center mt20">
        <el-link :href="qrcode_url" target="_blank" :underline="false">
          <el-image class="qrcode" :src="qrcode_url">
            <template #error>
              <div class="image-slot">
                加载中
              </div>
            </template>
          </el-image>
        </el-link>
      </div>
    </el-dialog>

    <el-dialog v-model="dialogVisible2" title="短海报" width="400" center>
      <div class="text-center mt20">
        <el-link :href="placard_url" target="_blank" :underline="false">
          <el-image class="placard" :src="placard_url">
            <template #error>
              <div class="image-slot">
                加载中
              </div>
            </template>
          </el-image>
        </el-link>
      </div>
    </el-dialog>

    <el-dialog v-model="dialogVisible3" title="长海报" width="400" center>
      <div class="text-center mt20">
        <el-link :href="dm_url" target="_blank" :underline="false">
          <el-image class="dm" :src="dm_url">
            <template #error>
              <div class="image-slot">
                加载中
              </div>
            </template>
          </el-image>
        </el-link>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="member-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('member')

  const organizationList = ref([])
  const roleList = ref([])
  const verifyStatusList = ref([])

  const dialogVisible1 = ref(false)
  const dialogVisible2 = ref(false)
  const dialogVisible3 = ref(false)

  const qrcode_url = ref('')
  const placard_url = ref('')
  const dm_url = ref('')

  const dataForm = reactive([
    'username',
    'nickname',
    'role_id',
    'organization_id',
    'market_status',
    'verify_status',
    'last_login_time'
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadRoleList = () => {
    $http({
      url: $http.adornUrl('/role/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        roleList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadVerifyStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'certification_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        verifyStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadQrcodeUrl = (id) => {
    $http({
      url: $http.adornUrl('/member/setting/data'),
      method: 'get',
      params: $http.adornParams({
        member_id: id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        qrcode_url.value = data.data.qrcode_url
        placard_url.value = data.data.placard_url
        dm_url.value = data.data.dm_url
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    if(query.keyword) {
      dataForm.keyword = query.keyword
    }

    getDataList(model)

    loadOrganizationList()
    loadRoleList()
    loadVerifyStatusList()
  })
</script>

<style scoped lang="scss">
  .qrcode {
    width: 10vw;
    height: 10vw;
  }
  .placard {
    width: 10vw;
    height: 15vw;
  }
  .dm {
    width: 10vw;
    height: 18vw;
  }
</style>
